package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.HouseBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.UserPermission;
import com.sx.zongzhi.fragment.HouseBasicInforFragment;
import com.sx.zongzhi.fragment.HouseRenterFragment;
import com.sx.zongzhi.presenter.FactHouseDetailPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IFactHouseDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 实有房屋/出租房详情
 */
public class FactHouseDetailActivity extends BaseActivity implements IFactHouseDetailView {

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<Fragment> fragments=null;

    private HouseBasicInforFragment houseBasicInforFragment;
    private HouseRenterFragment houseRenterFragment;

    private String id;
    private Disposable disposable;
    private boolean isUpdate = false;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        setTitleName("实有房屋详情");
        ButterKnife.bind(this);
        //根据权限判断要不要显示
        UserPermission userPermission = (UserPermission) getIntent().getSerializableExtra("userPermission");
         int houseType=getIntent().getIntExtra("HouseType",0);
        if (houseType==0){//实有房屋
            if (userPermission != null && userPermission.isDeleteActualHouse()){
                setRightImage(R.mipmap.icon_nav_delete);
            }
            if (userPermission != null && userPermission.isUpdateActualHouse()){
                setRightImage2(R.mipmap.icon_nav_edit);
            }
        }else {//出租房屋
            if (userPermission != null && userPermission.isDeleteRentalHouse()){
                setRightImage(R.mipmap.icon_nav_delete);
            }
            if (userPermission != null && userPermission.isUpdateRentalHouse()){
                setRightImage2(R.mipmap.icon_nav_edit);
            }
        }
        id = getIntent().getStringExtra("id");
        initRx();
        LoadDialog.show(FactHouseDetailActivity.this,"加载中...");
        new FactHouseDetailPest(this).getFactHouseDetail(this,id);
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if (bean.getRequest().equals("FactHouseDetailActivity")) {
                            isUpdate = true;
                            LoadDialog.show(FactHouseDetailActivity.this,"加载中...");
                            new FactHouseDetailPest(FactHouseDetailActivity.this).getFactHouseDetail(FactHouseDetailActivity.this,id);
                        }
                    }
                });
    }

    private void initView() {
        List<String> titles=new ArrayList<>();
        titles.add("基本信息");
        titles.add("住房成员");
        fragments=new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putSerializable("houseBean",houseBean);

        houseBasicInforFragment = new HouseBasicInforFragment();
        houseBasicInforFragment.setArguments(bundle);

        houseRenterFragment = new HouseRenterFragment();
        houseRenterFragment.setArguments(bundle);

        fragments.add(houseBasicInforFragment);
        fragments.add(houseRenterFragment);
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    @OnClick({R.id.imv_Right,R.id.imv_Right2})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                if (houseBean == null){
                    ToastUtils.showToast(FactHouseDetailActivity.this,"不可修改");
                    return;
                }
                startActivity(new Intent(this,UpdateFactHouseActivity.class).putExtra("houseBean",houseBean));
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除此房屋信息吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.show(FactHouseDetailActivity.this,"删除中...");
                new FactHouseDetailPest(FactHouseDetailActivity.this)
                        .deleteFactHouse(FactHouseDetailActivity.this,id);
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    private HouseBean houseBean;
    @Override
    public void getFactHouseDetailSuccess(HouseBean houseBeans) {
        LoadDialog.dismiss(FactHouseDetailActivity.this);
        houseBean = houseBeans;
        if (isUpdate){
            houseBasicInforFragment.initData(houseBean);
            houseRenterFragment.initData(houseBean);
        }else {
            initView();
        }
    }
    @Override
    public void deleteFactHouseSuccess(Boolean deleteResult) {
        LoadDialog.dismiss(FactHouseDetailActivity.this);
        ToastUtils.showToast(FactHouseDetailActivity.this,"删除成功");
        MyRxBus.get().post(new MsgEvent("FactHouseActivity",1,null));
        finish();
    }
    @Override
    public void showError(String message) {
        LoadDialog.dismiss(FactHouseDetailActivity.this);
        ToastUtils.showToast(FactHouseDetailActivity.this,message);
        if (houseRenterFragment == null){
            initView();
        }
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }
}
